package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.HotelRoomType;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class RoomTypeBean extends AbstractBean {
	public static final String BEAN_NAME="roomTypeBean";
	private MasterSetupService service;
	private List<HotelRoomType> roomTypeList;
	private HotelRoomType roomType;
	public static final String NAV_LIST_HOTELROOMTYPE="listRoomType";
	public static final String NAV_ADD_HOTELROOMTYPE="addRoomType";
	public static final String NAV_EDIT_HOTELROOMTYPE="editRoomType";
	 
	public MasterSetupService getService() {
		return service;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}
	public HotelRoomType getRoomType() {
		return roomType;
	}
	public void setRoomType(HotelRoomType roomType) {
		this.roomType = roomType;
	}
	public List<HotelRoomType> getRoomTypeList() {
		try{
			roomTypeList = service.getHotelRoomTypeList();
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return roomTypeList;
	}
	public void setRoomTypeList(List<HotelRoomType> roomTypeList) {
		this.roomTypeList = roomTypeList;
	}
	
	public String navCreationAction(){
		roomType = new HotelRoomType();
		return NAV_ADD_HOTELROOMTYPE;
	}
	
	public String createAction(){
		try{
			roomType.setCreatedBy(getThisUser());
			roomType.setCreatedDate(new Date());
			roomType.setIsDeleted(CommonConstant.NOT_DELETED);
			service.saveHotelRoomType(roomType);
			return NAV_LIST_HOTELROOMTYPE;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_ADD_HOTELROOMTYPE;
	}
	
	public void editListener(ActionEvent actionEvent){
		roomType = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("roomTypeId");
		String roomTypeId = (String)parameter.getValue();
		for(HotelRoomType a:roomTypeList){
			if(a.getHotelRoomTypeId().equalsIgnoreCase(roomTypeId)){
				roomType = a;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT_HOTELROOMTYPE;
	}
	
	public String editAction(){
		try{
			roomType.setModifiedBy(getThisUser());
			roomType.setModifiedDate(new Date());
			service.updateHotelRoomType(roomType);
			return NAV_LIST_HOTELROOMTYPE;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return NAV_EDIT_HOTELROOMTYPE;
	}
	
	public String deleteAction(){
		try{
			
			List<HotelRoomType> deleteList = new ArrayList<HotelRoomType>();
			for(HotelRoomType a:roomTypeList){
				if(a.isMarkAsAction()){
					deleteList.add(a);
				}
			}
			service.deleteHotelRoomTypes(deleteList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_LIST_HOTELROOMTYPE;
	}
}
